//
//  BarView.m
//  中皮网
//
//  Created by ios程序员 on 15/10/31.
//  Copyright (c) 2015年 ios程序员. All rights reserved.
//

#import "BarView.h"

@interface BarView ()<UISearchBarDelegate,UIScrollViewDelegate>

@property (nonatomic ,strong) UIButton *button;
@end

@implementation BarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加app名字图片
        self.nameImage = [[UIImageView alloc] initWithFrame:CGRectMake(WI * 10 / 375, HI * 30 / 667, WI * 80 / 375, HI * 30 / 667)];
        
        [self addSubview:self.nameImage];
        
        //添加滚动button下的scrollerView
        self.buttonScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, HI * 70 / 667, WI * 330 / 375, HI * 350/ 667)];
        self.buttonScrollView.backgroundColor = [UIColor whiteColor];
        self.buttonScrollView.contentSize = CGSizeMake(WI * 305 / 375, 0);
        self.buttonScrollView.bounces = NO;
        self.buttonScrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.buttonScrollView];
        
        //添加tableView下的scrollerView
        self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, HI * 105 / 667, WI , HI - HI * 95 / 667)];
//        self.bigScrollView.backgroundColor = [UIColor colorWithRed:1.000 green:0.485 blue:0.545 alpha:1.000];
        self.bigScrollView.contentSize = CGSizeMake(WI * 5, 0);
        self.bigScrollView.delegate = self;
        self.bigScrollView.pagingEnabled = YES;
        [self addSubview:self.bigScrollView];
        
        //添加滚动button
        NSArray *nameArr = @[@"综合排序", @"人气", @"销量", @"价格", @"筛选"];
        for (int i = 0; i < 5; i++) {
            
            self.titleButton = [UIButton buttonWithType:UIButtonTypeSystem];
            //self.titleButton.frame = CGRectMake(0 + 76 * i, H * 5 / 667, W * 76 / 375, H * 30 / 667);
            self.titleButton.frame = CGRectMake(0 + (WI * 66 / 375) * i, HI * 5 / 667, WI * 66 / 375, HI * 30 / 667);
            
            [self.titleButton setTitle:nameArr[i] forState:UIControlStateNormal];
            self.titleButton.tintColor = [UIColor blackColor];
            self.titleButton.titleLabel.font = [UIFont systemFontOfSize:13];
            [self.buttonScrollView addSubview:self.titleButton];
            self.titleButton.tag = 100 + i;
            [self.titleButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            
            if (i == 0) {
                self.tempButton = self.titleButton;
                self.tempButton.tintColor = [UIColor colorWithRed:1.000 green:0.304 blue:0.346 alpha:1.000];
                _tempButton.titleLabel.font = [UIFont systemFontOfSize:14];
            }
        }
        
//        //添加搜索button
//        self.searchbutton = [UIButton buttonWithType:UIButtonTypeSystem];
//        self.searchbutton.frame = CGRectMake(W * 330 / 375, H * 70 / 667, W - W * 330 / 375, H * 35 / 667);
//        [self.searchbutton setImage:[UIImage imageNamed:@"shouye-dianji.png"] forState:UIControlStateNormal];
//        self.searchbutton.backgroundColor = [UIColor whiteColor];
////        [self addSubview:self.searchbutton];
//        
//        [self.searchbutton addTarget:self action:@selector(searchbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //添加弹出的SeachBar
        self.search = [[UISearchBar alloc] initWithFrame:CGRectMake( 40, HI * 25 / 667, WI * 300 / 375, HI * 32 / 667)];
        self.search.layer.borderWidth = 0;
        self.search.keyboardType = 1;
        self.search.placeholder = @"商品搜索OR品牌";
        [self.search setTranslucent:YES];
        
        self.search.searchBarStyle = 2;
        self.search.searchResultsButtonSelected = YES;
        
        [self addSubview:self.search];
        self.search.delegate = self;
        
        //添加seacchBar前面的图片
        self.carImage = [[UIImageView alloc] initWithFrame:CGRectMake(WI, HI * 35 / 667, WI * 32 / 375, HI * 32 / 667)];
        self.carImage.image = [UIImage imageNamed:@""];
        [self addSubview:self.carImage];
        
    }
    
    return self;
}

////搜索button点击方法
//- (void)searchbuttonClick:(UIButton *)button
//{
//    button.selected = !button.selected;
//    
//        if (button.selected == 1) {
//            [self.searchbutton setImage:[UIImage imageNamed:@"shouye-dianji.png"] forState:UIControlStateNormal];
//            
//        }
//        else
//        {
//            
//            [self.searchbutton setImage:[UIImage imageNamed:@"shouye-dianji.png"] forState:UIControlStateNormal];
//
//        }
//
//}

//滑动button点击方法(bigScrollView 随button动)
- (void)buttonClick:(UIButton *)button
{
    _tempButton.titleLabel.font = [UIFont systemFontOfSize:13];
    _tempButton.tintColor = [UIColor blackColor];
    
    button.tintColor = [UIColor colorWithRed:1.000 green:0.304 blue:0.346 alpha:1.000];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    _tempButton = button;
    
    [self.bigScrollView setContentOffset:CGPointMake((button.tag - 100) * WI , 0) animated:NO];
    
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
